package com.stu.servlet;

import com.stu.dao.impl.TeacherDaoImpl;
import com.stu.model.StudentAnswer;
import com.stu.model.Task;
import com.stu.model.User;
import com.stu.util.DaoUtile;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/TeacherServlet")
public class TeacherServlet extends HttpServlet {
    @Override
    public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        //获取用户的动作
        String action = req.getParameter("action");
        //创建TeacherDao的对象
        TeacherDaoImpl teacherDao = new TeacherDaoImpl();
        //获取老师的对像
        User teacherUser = (User) req.getSession().getAttribute("session_newUser");
        //从老师对象中获取老师的账户
        String teacherAccount = teacherUser.getUserAccount();
        /*        System.out.println(teacherAccount);*/
        //动作为查询自己的学生即点击了"学生管理"或"查询"按钮或"删除学生按钮
        if (action.equals("studentList") || action.equals("searchMyStudent") || action.equals("deleteThisPersonByTeacher")) {
            //查询学生信息
            List<User> userList = null;
            if (action.equals("searchMyStudent")) {
                String searchText = req.getParameter("searchText");
                try {
                    userList = teacherDao.searchStudentByTeacherAccount(teacherAccount, searchText);
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
               //老师删除该学生与自己的联系
            } else {
                if (action.equals("deleteThisPersonByTeacher")) {
                    String studentAccount=req.getParameter("studentAccount");
                    try {
                        teacherDao.deleteThisPersonByTeacher(studentAccount,teacherAccount);
                    } catch (SQLException e) {
                        throw new RuntimeException(e);
                    }
                }
                //查询所有学生的信息
                try {
                    userList = teacherDao.SelectAllStudentByTeacherAccount(teacherAccount);
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            //加载页面
            /*   System.out.println(studentList);*/
            req.setAttribute("userList", userList);
            req.setAttribute("mainRight", "user.jsp");
            req.getRequestDispatcher("mainPage.jsp").forward(req, resp);
           //点击任务管理按钮或者删除任务按钮
        } else if (action.equals("goMyTask") || action.equals("deleteThisTask")) {
            //TeacherServlet?action=goMyTask
            if (action.equals("deleteThisTask")) {
                String taskAccount = req.getParameter("taskAccount");
                try {
                    //根据任务账户删除该任务
                    teacherDao.deleteThisTask(taskAccount);
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            List<Task> taskList = null;
            try {
                //获取所有的任务
                taskList = teacherDao.getAllMyTask(teacherAccount);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            /*   System.out.println(taskList.get(0).getTaskAccount());*/
            //加载新的页面
            req.setAttribute("taskList", taskList);
            req.setAttribute("mainRight", "task.jsp");
            req.getRequestDispatcher("mainPage.jsp").forward(req, resp);


            //goSkimDetail:任务管理->查看详情->得到该题目被发送隶属自己课程的同学,并可以看到他们完成任务的情况.针对老师
            //goStudentTaskDetail:学生管理->详细作业->得该同学的所有作业情况,针对具体的同学
        } else if (action.equals("goSkimDetail") || action.equals("goStudentTaskDetail")) {
            List<StudentAnswer> studentAnswerList = null;
            if (action.equals("goSkimDetail")) {
                String taskAccount = req.getParameter("taskAccount");
                /*  System.out.println(taskAccount);*/
                try {
                    //根据任务账户查询任务的具体情况
                    studentAnswerList = teacherDao.getTaskDetail(taskAccount);
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            } else {
                String studentAccount = req.getParameter("studentAccount");
                try {
                    //根据学生账户查询学生账户的具体情况
                    studentAnswerList = teacherDao.goStudentTaskDetail(teacherAccount, studentAccount);
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
             //拿着这些作答情况重新加载新的页面
            req.setAttribute("studentAnswerList", studentAnswerList);
            req.setAttribute("mainRight", "studentT.jsp");
            req.getRequestDispatcher("mainPage.jsp").forward(req, resp);
           //任务管理->查看详情->删除任务
        } else if (action.equals("deleteTask")) {
            String studentAccount = req.getParameter("studentAccount");
            String taskAccount = req.getParameter("taskAccount");
            String isFinish = req.getParameter("isFinish");
            try {
                //根据学生账户和人户账户以及作答情况删除该任务
                teacherDao.deleteTask(taskAccount, studentAccount, isFinish);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            req.setAttribute("tip", "删除成功");
            req.setAttribute("mainRight", "blank.jsp");
            req.getRequestDispatcher("mainPage.jsp").forward(req, resp);
            //添加学生的任务即点击了"添加任务"或"添加"按钮
        } else if (action.equals("goAddTask") || action.equals("addTask")) {
            if (action.equals("addTask")) {
                //获取老师添加的任务的内容
                String taskName = req.getParameter("taskName");
                taskName = taskName.trim();
                if (taskName.trim().equals("")) {
                    req.setAttribute("tip", "添加失败");
                } else {
                    //添加数据到数据库
                    String date = DaoUtile.getDate();
                    String taskAccount = null;
                    try {
                        //随机生成任务的账户
                        taskAccount = DaoUtile.getTaskAccount();
                    } catch (SQLException e) {
                        throw new RuntimeException(e);
                    }
                    //然后创建一个任务对象
                    Task task = new Task(taskAccount, teacherAccount, date, taskName);
                    try {
                        teacherDao.addTask(task);
                    } catch (SQLException e) {
                        throw new RuntimeException(e);
                    }
                    req.setAttribute("tip", "添加成功");
                }
            }
            req.setAttribute("mainRight", "addTask.jsp");
            req.getRequestDispatcher("mainPage.jsp").forward(req, resp);
        }
    }


}
